package jlt.SunTime;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.text.format.DateFormat;

import java.util.Calendar;

public class SunTimeActivity extends Activity implements LocationListener{
	
	private LocationManager mgr;
	private String best;
	private TextView TextViewLatitude;
	private TextView TextViewLongitude;
	private TextView TextViewSunrise;
	private TextView TextViewSunset;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mgr = (LocationManager)getSystemService(LOCATION_SERVICE);
        TextViewLatitude = (TextView)findViewById(R.id.TextViewLatitude);
        TextViewLongitude = (TextView)findViewById(R.id.TextViewLongitude);
        TextViewSunrise = (TextView)findViewById(R.id.TextViewSunrise);
        TextViewSunset = (TextView)findViewById(R.id.TextViewSunSet);
        
        Criteria criteria = new Criteria();
        best = mgr.getBestProvider(criteria, true);
        Location location = mgr.getLastKnownLocation(best);
        if (location != null)
        	printLocation(location);
        if (location != null)
        	printSunriseSunset(location);
    }
    
    @Override
    protected void onResume()
    {
    	super.onResume();
    	mgr.requestLocationUpdates(best, 1500, 1, this);
    }
    
    @Override
    protected void onPause()
    {
    	super.onPause();
    	mgr.removeUpdates(this);
    }
    
    private void printLocation(Location location)
    {
    	if (location != null)
    	{
    		TextViewLatitude.setText(Double.toString(location.getLatitude()));
    		TextViewLongitude.setText(Double.toString(location.getLongitude()));
    	}
    	else
    	{
    		TextViewLatitude.setText("");
    		TextViewLongitude.setText("");
    	}
    }
    
    private void printSunriseSunset(Location location)
    {
    	// Code in here might better be moved to the SunTime class
    	if (location != null)
    	{
    
    		java.text.DateFormat dateFormat = DateFormat.getTimeFormat(this);
		
    		Calendar cal = Calendar.getInstance();
    		cal.set(Calendar.HOUR_OF_DAY, 0);
    		cal.set(Calendar.MINUTE, 0);
    		cal.set(Calendar.SECOND, 0);
		
    		Calendar cal2 = Calendar.getInstance();
    		cal2.set(Calendar.HOUR_OF_DAY, 0);
    		cal2.set(Calendar.MINUTE, 0);
    		cal2.set(Calendar.SECOND, 0);
		
    		SunTime st = new SunTime(location.getLatitude(),location.getLongitude(),0,null,cal);
		
    		int sunriseSec = st.getRiseTimeSec();
    		int sunsettimeSec = st.getSetTimeSec();
		
    		Calendar riseTime = cal;
    		riseTime.add(Calendar.SECOND, sunriseSec);
		
    		Calendar setTime = cal2;
    		setTime.add(Calendar.SECOND, sunsettimeSec);
		
    		//System.out.println("Sunrise = " + sunriseSec);
    		//System.out.println("Sunset = " + sunsettimeSec);
    		
    		TextViewSunrise.setText(dateFormat.format(riseTime.getTime()));
    		TextViewSunset.setText(dateFormat.format(setTime.getTime()));
		
    		//System.out.println("The sun will rise at " + dateFormat.format(riseTime.getTime()));
		
    		//System.out.println("The sun will set at " + dateFormat.format(setTime.getTime()));	
    	}
    }

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		if (location != null)
			printLocation(location);
		if (location != null)
			printSunriseSunset(location);
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}
}